#include "com_form_dept.h"
#include "ui_com_form_dept.h"

#include "pub_var.h"
#include <QSettings>
#include <QMessageBox>
#include <QSqlError>


Form_dept::Form_dept(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_dept)
{
    ui->setupUi(this);

    initialized = false;
    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/dept").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);
    md = new QSqlQueryModel();

    sq = "select name, code from com_dictionary where type ='DeptCategory' ";
    dept_types = pub::fillCombo(ui->cbb_dept_type, sq, -1, qro);

    sq = " select concat(concat( dic_label ,'^'), dic_value) "
         " from com_dict_new"
         " where dic_type = 'caty' and dic_value like 'A%' "
         " order by dic_value";
    ui->cbb_standard_dept->addItem("");
    pub::fillCombo(ui->cbb_standard_dept, sq, -1, qrm, false);

    tm_init = new QTimer(this);
    connect(tm_init, &QTimer::timeout, this, &Form_dept::tm_init_out);

    initialized = true;

    tm_init->start(300);

}

Form_dept::~Form_dept()
{
    delete ui;
}

void Form_dept::tm_init_out()
{
    tm_init->stop();
    on_pb_fresh_clicked();
}
void Form_dept::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/dept", QVariant(this->geometry()));

    emit destroyW( "dept_manage" );

}

void Form_dept::on_pb_exit_clicked()
{
    close();
}

void Form_dept::on_pb_fresh_clicked()
{
    sq = "select dep.dept_code 编码, dep.dept_name 名称, dic.name 类别，"// 0 - 2
         " dep.dept_type, dep.spell_code, dep.regdept_flag, dep.tatdept_flag, "// 3 - 6
         " dep.valid_state, dep.default_diag, concat(concat( standard_name ,'^'), standard_code) "// 7 - 9
         " from com_department dep ";
    sq += " left join com_dictionary dic on dic.code = dep.dept_type and dic.type ='DeptCategory' ";
qDebug()<<"select department code, name, type : "<<sq;
    if(!ui->ckb_all->isChecked())
    {
        sq += " where dep.valid_state = '1' ";
    }
    sq += " order by dep.dept_name";
    int w = ui->tv_dept_list->width();
    md->setQuery(sq, pub::db_o);
    ui->tv_dept_list->setModel(md);
    pub::setColumnWidth(ui->tv_dept_list, "0.25^0.3^0.3^0^0^0^0^0^0^0^0", w);
}

void Form_dept::on_tv_dept_list_clicked(const QModelIndex &index)
{
    if(!initialized)
    {
        return;
    }

    ui->le_dept_code->setText(index.siblingAtColumn(0).data().toString());
    ui->le_dept_name->setText(index.siblingAtColumn(1).data().toString());
    ui->le_spell->setText(index.siblingAtColumn(4).data().toString());

    ui->cbb_dept_type->setCurrentText(index.siblingAtColumn(2).data().toString());

    if(index.siblingAtColumn(5).data().toString() == "1")
    {
        ui->ckb_is_reg->setChecked(true);
    }else{
        ui->ckb_is_reg->setChecked(false);
    }

    if(index.siblingAtColumn(6).data().toString() == "1")
    {
        ui->ckb_is_calc->setChecked(true);
    }else{
        ui->ckb_is_calc->setChecked(false);
    }

    if(index.siblingAtColumn(7).data().toString() == "1")
    {
        ui->ckb_is_valid->setChecked(true);
    }else{
        ui->ckb_is_valid->setChecked(false);
    }

    ui->le_default_diag->setText(index.siblingAtColumn(8).data().toString());
    QString t = index.siblingAtColumn(9).data().toString();
    qDebug()<<t;
    ui->cbb_standard_dept->setCurrentIndex(0);
    ui->cbb_standard_dept->setCurrentText(t);

    is_new = false;
}

void Form_dept::on_tv_dept_list_doubleClicked(const QModelIndex &index)
{
    on_tv_dept_list_clicked(index);
}

void Form_dept::on_pb_new_clicked()
{

   ui->pb_new->setEnabled(false);
    clear_form();

    qro->exec("select max(dept_code) from com_department");
    qro->next();

    QString str = QString::number(qro->value(0).toInt() + 1);
    qDebug()<<"new dept code : "<<str;
    QString new_dept_code ;
    new_dept_code = QString("0000").replace( 4 - str.length(), str.length(), str);
    ui->le_dept_code->setText(new_dept_code);
    is_new = true;
}

void Form_dept::clear_form()
{
    ui->le_dept_code->clear();
    ui->le_dept_name->clear();
    ui->le_spell->clear();
    ui->cbb_dept_type->setCurrentIndex(-1);
    ui->cbb_standard_dept->setCurrentIndex(-1);
    ui->ckb_is_reg->setChecked(false);
    ui->ckb_is_calc->setChecked(false);
    ui->ckb_is_valid->setChecked(false);
    ui->le_default_diag->clear();
    ui->le_default_diagname->clear();

}

void Form_dept::on_pb_save_clicked()
{

    int pos;
    QString dept_name = ui->le_dept_name->text().trimmed();

    if(dept_name.isEmpty())
    {
        QMessageBox::information(this, "注意", "诊室名称不可以为空白！");
        return;
    }

    if(is_new)
    {
        sq = "select dept_name from com_department where dept_name = '" + dept_name + "'";
        qro->exec(sq);

        if(qro->next())
        {
            QMessageBox::information(this, "注意", "诊室名称 " + dept_name + " 已经存在！");
            return;
        }
    }

    if(ui->le_dept_code->text().trimmed().isEmpty())
    {
        QMessageBox::information(this, "注意", "诊室代码不可以为空白！");
        return;
    }

    //诊室类别
    if(ui->cbb_dept_type->currentText().isEmpty())
    {
        QMessageBox::information(this, "注意", "诊室类别不可以为空白！");
        return;
    }

    //标准科目编码与名称
    QString st_code, st_name;
    if(!ui->cbb_standard_dept->currentText().isEmpty())
    {
        QString t = ui->cbb_standard_dept->currentText();
        st_name = pub::getStr(t, "^", 1);
        st_code = pub::getStr(t, "^", 2);
    }

    //默认诊断
    if(!ui->le_default_diag->text().trimmed().isEmpty()
            && ui->le_default_diagname->text().trimmed().isEmpty())
    {
        QMessageBox::information(this, "注意", "默认诊断代码无效！");
        return;
    }
    pos = pub::searchStr(dept_types, "^", ui->cbb_dept_type->currentText());
    QString dept_type = pub::getStr(dept_types, "^", pos + 1);

    QString is_reg, is_calc, is_valid;

    if(ui->ckb_is_reg->isChecked())
    {
        is_reg = "1";
    }else{
        is_reg = "0";
    }

    if(ui->ckb_is_calc->isChecked())
    {
        is_calc = "1";
    }else{
        is_calc = "0";
    }

    if(ui->ckb_is_valid->isChecked())
    {
        is_valid = "1";
    }else{
        is_valid = "0";
    }

    if(is_new)
    {
        //insert

        sq = "insert into com_department "
             "(dept_code, dept_name, spell_code, "//0-2
             "dept_type, regdept_flag, tatdept_flag, valid_state,"//3-6
             "oper_code, oper_date, default_diag,"//7-9
             "standard_name, standard_code )";//10-11
        sq += " values(";
        //0-2
        sq += "'" + ui->le_dept_code->text() + "', ";
        sq += "'" + dept_name + "', ";
        sq += "'" + ui->le_spell->text().toUpper() + "', ";
        //3-6
        sq += "'" + dept_type + "', ";
        sq += "'" + is_reg + "', ";
        sq += "'" + is_calc + "', ";
        sq += "'" + is_valid + "', ";
        //7-9
        sq += "'" + pub::usr_code + "', ";
        sq += "%1,";
        sq += "'" + ui->le_default_diag->text() + "',";
        //10-11
        sq += "'" + st_name + "', ";
        sq += "'" + st_code + "' ";
        sq += ")";
    }
    else
    {
        //update
        sq = "update com_department set ";
        sq += " spell_code = '" + ui->le_spell->text() + "', ";
        sq += " dept_type = '" + dept_type + "', ";
        sq += " regdept_flag = '" + is_reg + "', ";
        sq += " tatdept_flag = '" + is_calc + "', ";
        sq += " valid_state = '" + is_valid + "', ";
        sq += " oper_code = '" + pub::usr_code + "', ";
        sq += " standard_name = '" + st_name + "', ";
        sq += " standard_code = '" + st_code + "', ";
        sq += " oper_date = %1, ";
        sq += " default_diag = '" + ui->le_default_diag->text() + "' ";
        sq += " where dept_code = '" + ui->le_dept_code->text() + "'";
    }


    if(!qro->exec(sq.arg("sysdate")))
    {
        QMessageBox::information(this, "注意", " oracle 操作失败！\n" + qro->lastError().text());
        qDebug()<<"oracle诊室增加或修改："<<sq;
        return;
    }

    if(!qrm->exec(sq.arg("now()")))
    {
        QMessageBox::information(this, "注意", " mysql 操作失败！\n" + qro->lastError().text());
        qDebug()<<"mysql诊室增加或修改："<<sq;
        return;
    }

    clear_form();
    QMessageBox::information(this, "注意", " 操作成功！");
    ui->pb_new->setEnabled(true);
}

void Form_dept::on_le_default_diag_textChanged(const QString &arg1)
{
    if(arg1.trimmed().isEmpty())
    {
        return;
    }
    sq = "select 诊断名称 from new_down_1307 where 诊断代码 = '" + arg1.trimmed() + "'";
    qrm->exec(sq);
    qrm->next();
    if(!qrm->value(0).toString().isEmpty())
    {
        ui->le_default_diagname->setText(qrm->value(0).toString());
    }
}
